package demo;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.component.UIData;
import javax.faces.component.UIInput;
import javax.faces.component.UISelectBoolean;
import javax.faces.context.FacesContext;
import javax.faces.validator.ValidatorException;




public class BankBackingBean {
	
	List<Customer> customers = new ArrayList<Customer>();
	
	public BankBackingBean() {
		super();
		customers.add(new Customer("Jean","Claude"));
		customers.add(new Customer("Dupon","Pierre"));
		customers.add(new Customer("Paul","Michel"));
	}
	
	
	public static final String KEY_BOTH_INVALID = "message.validation.croisee.impossible";

	private UIData datatable;
	private UISelectBoolean checkbox;
	private UIInput numeroCompteInput;
	private UIInput categorieInput;

	public List<Customer> getCustomers() {
		return customers;
	}

	public void setCustomers(List<Customer> customers) {
		this.customers = customers;
	}


	public void deleteCustomer() {
		int size = this.datatable.getRowCount();
		List<Customer> selectedCustomers = new ArrayList<Customer>();
		for (int i = 0; i < size; i++) {
			this.datatable.setRowIndex(i);
			if (this.checkbox.isSelected()) {
				selectedCustomers.add(this.customers.get(i));
			}
		}
		this.customers.removeAll(selectedCustomers);
	}

	public void addCustomer() {
		Customer customer = new Customer();
		customer.setName("Nouveau");
		customer.setForName("client");
		this.customers.add(customer);
	}	
	

	public String submit(){
        System.out.println(customers.get(0));
		return "ok";
	}

	public UIData getDatatable() {
		return datatable;
	}

	public void setDatatable(UIData datatable) {
		this.datatable = datatable;
	}

	public UISelectBoolean getCheckbox() {
		return checkbox;
	}

	public void setCheckbox(UISelectBoolean checkbox) {
		this.checkbox = checkbox;
	}
	
	boolean numCompteValide(String valeur) {
		Pattern p = Pattern.compile("[0-9][0-9]-[0-9][0-9][0-9]",
				Pattern.MULTILINE);
		Matcher m = p.matcher(valeur);
		return m.matches();
	}
	
	boolean categorieValide(String valeur) {
		return ("7".equals(valeur));
	}

	
	boolean estValide(String num, String categ){
		return numCompteValide(num) && categorieValide(categ);
	}
	
	public void validerNumCompteCateg(FacesContext contexte, UIComponent composant,
			Object objet) {
		boolean estValide = false;
		if (numeroCompteInput.getLocalValue()!=null && categorieInput.getLocalValue()!=null)
		   estValide = estValide(numeroCompteInput.getLocalValue().toString(),categorieInput.getLocalValue().toString());
		if (!estValide) {
			FacesMessage errMsg = MessageFactory.getMessage("i18n.Messages",contexte,
					KEY_BOTH_INVALID);
			throw new ValidatorException(errMsg);
		}
	}

	public UIInput getNumeroCompteInput() {
		return numeroCompteInput;
	}

	public void setNumeroCompteInput(UIInput numeroCompteInput) {
		this.numeroCompteInput = numeroCompteInput;
	}

	public UIInput getCategorieInput() {
		return categorieInput;
	}

	public void setCategorieInput(UIInput categorieInput) {
		this.categorieInput = categorieInput;
	}	

}
